<template>
    <div>
        <div><h1>商品审核</h1></div>
        <div>
            <el-form
                :inline="true"
                :model="searchForm"
                class="demo-form-inline"
            >
                <el-form-item label="关键字">
                    <el-input
                        v-model="searchForm.keyWord"
                        placeholder="请输入商品名称/ID/关键词"
                        clearable
                    />
                </el-form-item>
                <el-form-item label="订单状态">
                    <el-select
                        v-model="searchForm.status"
                        placeholder="请选择订单状态"
                        style="width: 160px;"
                        clearable
                    >
                        <el-option label="草稿" :value="0" />
                        <el-option label="待审核" :value="1" />
                        <el-option label="已上线" :value="2" />
                        <el-option label="已下线" :value="3" />
                        <el-option label="缺货" :value="4" />
                    </el-select>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="loadProdList">查询</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div>
            <el-table :data="prodList" stripe style="width: 100%">
                <el-table-column prop="prodId" label="ID" width="100" />
                <el-table-column prop="prodSpu" label="SPU" width="120" />
                <el-table-column prop="prodName" label="商品名称" width="120" >
                    <template #default="scope">
                        <span v-html="scope.row.prodName"></span>
                    </template>
                </el-table-column>
                <el-table-column prop="kindName" label="商品种类" width="120" />
                <el-table-column prop="typeName" label="商品分类" width="120" />
                <el-table-column prop="brandName" label="品牌" width="100" >
                    <template #default="scope">
                        <span v-html="scope.row.brandName"></span>
                    </template>
                </el-table-column>
                <el-table-column prop="unitName" label="单位" width="100" />
                <el-table-column prop="tagName" label="标签" width="100" />
                <el-table-column prop="appr" label="供应商" width="120" />
                <el-table-column prop="prodImage" label="图片" width="120">
                    <template #default="scope">
                        <img
                            :src="scope.row.prodImage"
                            alt=""
                            style="width: 50px; height: 50px"
                        />
                    </template>
                </el-table-column>
                <el-table-column prop="prodStatus" label="状态" width="100">
                    <template #default="scope">
                        <span v-if="scope.row.prodStatus == 0">草稿</span>
                        <span v-if="scope.row.prodStatus == 1">待审核</span>
                        <span v-if="scope.row.prodStatus == 2">已上线</span>
                        <span v-if="scope.row.prodStatus == 3">已下线</span>
                        <span v-if="scope.row.prodStatus == 4">缺货</span>
                    </template>
                </el-table-column>
                <el-table-column prop="createTime" label="时间" width="160" />
                <el-table-column label="操作" />
            </el-table>
        </div>
        <div style="margin-top: 20px">
            <el-pagination
                v-model:current-page="searchForm.pageNum"
                v-model:page-size="searchForm.pageSize"
                :page-sizes="[2, 4, 6, 8]"
                :background="true"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
            />
        </div>
    </div>
</template>

<script setup lang="ts">
import { getProdFromEs } from "@/api/api";
import { onMounted, ref } from "vue";

const prodList = ref([]);
const total = ref(0);
const searchForm = ref({
    pageNum: 1,
    pageSize: 2,
    keyWord: null,
    status: null
});

const handleSizeChange = (val: number) => {
    console.log(`${val} items per page`);
    searchForm.value.pageSize = val;
    loadProdList();
};
const handleCurrentChange = (val: number) => {
    console.log(`current page: ${val}`);
    searchForm.value.pageNum = val;
    loadProdList();
};

// 获取商品列表
const loadProdList = () => {
    getProdFromEs(searchForm.value).then((res) => {
        prodList.value = res.data.data.records;
        total.value = res.data.data.total;
    });
};

// 页面钩子函数
onMounted(() => {
    loadProdList();
});
</script>

<style scoped></style>
